/*
 * Copyright (c) 2011 Imre Fazekas.
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * Neither the name of the Brillien nor the names of its
 * terms and concepts may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package com.vii.streamline.services.reflection;

import com.vii.streamline.services.error.StreamLineException;

/**
 * Reflection-helper class referencing to a class and the enclosing object if exists (in case of inner class).
 */
public class ReflectionReference<M> {

    /**
     * MessageProcessor class reference
     */
    protected Class<M>      typeReference;

    /**
     * Singleton instance if instantiation need to be done somewhere else
     */
    protected M             singleton;

    /**
     * Enclosing object if exists
     */
    protected Object        outerInstance;

    /**
     * Tells whether this reference is referring to a static member
     */
    protected boolean       staticReference;

    public ReflectionReference(Class<M> typeReference) {
        this( typeReference, null );
    }

    public ReflectionReference(Class<M> typeReference, boolean staticReference) {
        this( typeReference, null, staticReference );
    }

    public ReflectionReference(Class<M> typeReference, boolean staticReference, M singleton) {
        this( typeReference, null, staticReference, singleton );
    }

    public ReflectionReference(Class<M> typeReference, Object outerInstance) {
        this( typeReference, outerInstance, false );
    }

    public ReflectionReference(Class<M> typeReference, Object outerInstance, boolean staticReference) {
        this( typeReference, outerInstance, staticReference, null );
    }
    public ReflectionReference(Class<M> typeReference, Object outerInstance, boolean staticReference, M singleton) {
        this.typeReference = typeReference;
        this.outerInstance = outerInstance;
        this.staticReference = staticReference;
        this.singleton = singleton;
    }

    /**
     * Get method for field messageProcessorType
     */
    public Class<M> getTypeReference() {
        return typeReference;
    }

    /**
     * Set method for field messageProcessorType
     */
    public void setTypeReference(Class<M> typeReference) {
        this.typeReference = typeReference;
    }

    /**
     * Get method for field outerInstance
     */
    public Object getOuterInstance() {
        return outerInstance;
    }

    /**
     * Set method for field outerInstance
     */
    public void setOuterInstance(Object outerInstance) {
        this.outerInstance = outerInstance;
    }

    /**
     * Is method for field staticReference
     */
    public boolean isStaticReference() {
        return staticReference;
    }

    /**
     * Set method for field staticReference
     */
    public void setStaticReference(boolean staticReference) {
        this.staticReference = staticReference;
    }

    /**
     * Get method for field singleton
     */
    public M getSingleton() {
        return singleton;
    }

    /**
     * Set method for field singleton
     */
    public void setSingleton(M singleton) {
        this.singleton = singleton;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ReflectionReference that = (ReflectionReference) o;

        if (typeReference != null ? !typeReference.equals(that.typeReference) : that.typeReference != null)
            return false;

        return true;
    }

    public M newInstance() throws StreamLineException {
        try {
            M m = singleton == null ? ReflectionServices.<M>newInstance( typeReference, outerInstance) : singleton;
            return m;
        } catch (Exception e) {
            throw new StreamLineException( e );
        }
    }

    @Override
    public String toString() {
        return "ReflectionReference{" +
                "typeReference=" + typeReference +
                ", outerInstance=" + outerInstance +
                '}';
    }

}
